tb = 1;
ns = 8;
ts = tb/ns;
fs = 1/ts;
BitNum = 100000;

t = ts:ts:BitNum*tb;
delay = 3;
delay_length = 3*ns;
ir_length = delay*ns*2 + 1;

function newSig = sigexpand(sig, n)
  newSig = [sig; zeros(n-1,size(sig, 2))];
  newSig = newSig(:)';
endfunction

msg = sign(rand(1, BitNum, 'normal'));
a = sigexpand(msg, ns);
g = filter_tap(1, ir_length, ns, 1, 1);
ag = convol(a, g);

//plot(t, a, t, ag(delay_length+1:$-delay_length))

EbN0_db = 0;
EbN0 = 10^(EbN0_db/10);
Eb = 1;
N0 = 1/EbN0;
sigma = sqrt(fs*N0/2);

noise = sigma*rand(ag, 'normal');
agn = ag + noise;
h = g / sqrt(ns);
r = convol(agn, h); // r is received signal.

offset = 1;
//plot(t, 3*a, t, r(2*delay_length+1+offset:$-2*delay_length+offset))
sampledR = r(ir_length+1:ns:$-ir_length);
rcv_msg = sign(sampledR);
errBitNum = length(find(msg ~= rcv_msg));
errBitNum / BitNum
